//
// Created by zhupi on 2024/10/1.
//

#include "message_dock.h"

#include <QPushButton>
#include <QVBoxLayout>
#include <QApplication>
#include <QScrollBar>

namespace {
    const QString ERROR_WIDGET_OBJECT_NAME = "message window item error";
    const QString WARRING_WIDGET_OBJECT_NAME = "message window item warring";
    const QString INFO_WIDGET_OBJECT_NAME = "message window item info";
}

MessageDock::MessageDock(QWidget *parent) : QDockWidget(parent){

}

void MessageDock::Initial() {
//    this->setStyleSheet("background-color: rgb(255, 0, 0);");
    this->setWindowTitle("Message");

    widget_ = new QWidget(this);
    scrollArea_ = new QScrollArea(this);
    scrollArea_->setWidget(widget_);
    scrollArea_->setWidgetResizable(true); // 确保内容的宽度适应滚动区域的宽度

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(0, 0, 0, 0); // 消除边距

    // 初始化上面的搜索栏
    InitToolWidget(mainLayout);
    mainLayout->addWidget(scrollArea_);
    QWidget *mainWidget = new QWidget(this);
    mainWidget->setLayout(mainLayout);
    this->setWidget(mainWidget);

    // 初始化消息窗口
    InitMessageWidget();
    // 信号连接
    InitConnect();
}

void MessageDock::InitMessageWidget( )
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(0, 0, 0, 0); // 消除边距
    // 创建三个折叠小部件
    errorWidget_ = new MessageItemWidget("error message");
    errorWidget_->setObjectName(ERROR_WIDGET_OBJECT_NAME);
    warningWidget_ = new MessageItemWidget("warrgin");
    warningWidget_->setObjectName(WARRING_WIDGET_OBJECT_NAME);
    infoWidget_ = new MessageItemWidget("info");
    infoWidget_->setObjectName(INFO_WIDGET_OBJECT_NAME);

    mainLayout->addWidget(errorWidget_);
    mainLayout->addWidget(warningWidget_);
    mainLayout->addWidget(infoWidget_);
    mainLayout->addStretch();
    widget_->setLayout(mainLayout);
}

void MessageDock::InitToolWidget(QVBoxLayout *layout )
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    // 查找框和按钮
    QHBoxLayout *searchLayout = new QHBoxLayout();
    searchLineEdit_ = new QLineEdit(this);
    searchButton_ = new QPushButton("Find", this);
    deleteButton_ = new QPushButton("Delete Select ALL", this);

    searchLayout->addWidget(searchLineEdit_);
    searchLayout->addWidget(searchButton_);
    searchLayout->addWidget(deleteButton_);
    mainLayout->addLayout(searchLayout);
    layout->addLayout(mainLayout);

    // 连接信号和槽
    connect(searchButton_, &QPushButton::clicked, this, &MessageDock::SearchMessage);
    connect(deleteButton_, &QPushButton::clicked, this, &MessageDock::DeleteAllMessage);
}

void MessageDock::SearchMessage()
{
    QString text = searchLineEdit_->text();
    if (text.isEmpty()) {
        return;
    }

    // 高亮查找的内容
    int totul = errorWidget_->HighlightMatchingItems(text);
    totul += warningWidget_->HighlightMatchingItems(text);
    totul += infoWidget_->HighlightMatchingItems(text);

    // 跳转并高亮指定行
    switch (searchIndex_) {
        case 0:
            if (!errorWidget_->SearchMessage(text)) {
                errorWidget_->UpdateSearch();
                searchIndex_ = 1;
            } else {
                break;
            }
        case 1:
            if (!warningWidget_->SearchMessage(text)) {
                warningWidget_->UpdateSearch();
                searchIndex_ = 2;
            } else {
                break;
            }

        case 2:
            if (!infoWidget_->SearchMessage(text)) {
                infoWidget_->UpdateSearch();
                searchIndex_ = 0;
            }else {
                break;
            }
    }
}

void MessageDock::DeleteAllMessage()
{
    QString text = searchLineEdit_->text();
    if (text.isEmpty()) {
        return;
    }
    errorWidget_->DeleteMessage(text);
    warningWidget_->DeleteMessage(text);
    infoWidget_->DeleteMessage(text);
}

void MessageDock::InitConnect()
{
    connect(errorWidget_, &MessageItemWidget::PositionChangeSg, this, &MessageDock::PositionChange);
    connect(warningWidget_, &MessageItemWidget::PositionChangeSg, this, &MessageDock::PositionChange);
    connect(infoWidget_, &MessageItemWidget::PositionChangeSg, this, &MessageDock::PositionChange);
}

void MessageDock::PositionChange(const QPoint &point)
{
    // 将该项的全局坐标映射到父窗口的内容区域坐标系
    QWidget* parentContent = scrollArea_->widget();
    QPoint parentContentPos = parentContent->mapFromGlobal(point);

    // 调整父窗口滚动区域的垂直滚动条
    int scrollValue = scrollArea_->verticalScrollBar()->value();
    int offset = parentContentPos.y();
    scrollArea_->verticalScrollBar()->setValue(scrollValue + offset);
}
